import 'package:air_audio/global.dart';
import 'package:air_audio/iconfont.dart';
import 'package:air_audio/manager.dart';
import 'package:air_audio/player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Provider.debugCheckInvalidValueType = null;

  runApp(
    MultiProvider(
      child: MyApp(),
      providers: [
        ChangeNotifierProvider<MainPlayerState>(
          create: (_) => MainPlayerState()..init(),
        ),
      ],
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: '悦音助眠',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  List<Widget> renderItems(items) {
    List<Widget> list = [];
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      list.add(AudioItem(item: item, index: i));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    MainPlayerState playerState = Provider.of<MainPlayerState>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '悦音助眠',
          style: TextStyle(
            color: Global.fontColor,
            fontSize: 13,
            fontWeight: FontWeight.bold,
          ),
        ),
        backgroundColor: Global.backgroundColor,
        brightness: Brightness.dark,
        elevation: 10,
        actions: <Widget>[
          IconButton(
            icon: Icon(
              IconFont.yinle,
              color: Global.fontColor,
            ),
            tooltip: '播放中',
            onPressed: () {
              if (playerState.playing == null) {
                playerState.setPlaying(Global.audios[0], 0);
              }
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => PlayerPage(),
                ),
              );
            },
          )
        ],
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          color: Global.backgroundColor,
        ),
        child: SingleChildScrollView(
          child: Column(
            children: renderItems(Global.audios),
          ),
        ),
      ),
    );
  }
}

class AudioItem extends StatefulWidget {
  AudioItem({Key key, this.item, this.index}) : super(key: key);
  final item;
  final int index;

  @override
  _AudioItemState createState() => _AudioItemState();
}

class _AudioItemState extends State<AudioItem> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    MainPlayerState playerState = Provider.of<MainPlayerState>(context);
    return InkWell(
      onTap: () {
        playerState.setPlaying(widget.item, widget.index);
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => PlayerPage(),
          ),
        );
      },
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: 100,
        decoration: BoxDecoration(),
        child: Row(
          children: [
            Container(
              width: 120,
              height: 100,
              child: Stack(
                children: [
                  Container(
                    width: 100,
                    height: 100,
                    child: Image.asset(
                      'assets/images/${widget.item['cover']}',
                      fit: BoxFit.cover,
                    ),
                  ),
                  Positioned(
                    child: Container(
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                          colors: [
                            Colors.transparent,
                            Colors.transparent,
                            Colors.transparent,
                            Colors.transparent,
                            Global.backgroundColor.withOpacity(.9),
                            Global.backgroundColor,
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(width: 20),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    widget.item['name'],
                    style: TextStyle(
                      color: Global.fontColor,
                      fontSize: 23,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  Row(
                    children: [
                      Icon(
                        Icons.play_circle_fill,
                        color: Global.fontColor,
                        size: 25,
                      ),
                      SizedBox(width: 3),
                      Text(
                        '立即播放',
                        style: TextStyle(
                          color: Global.fontColor,
                          fontSize: 17,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ],
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
